//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ImpliedStdDevQuote.h"
using namespace Cephei::QL::Quotes;
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (QL::Option::TypeEnum optionType, Cephei::QL::IQuote^ forward, Cephei::QL::IQuote^ price, Double strike, Double guess, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt32>^ maxIter) : CQuote(CImpliedStdDevQuote::typeid)
{
    CQuote^ _Cforward;
    CQuote^ _Cprice;
    try
    {
#ifdef HANDLE
        _phImpliedStdDevQuote = NULL;
#endif
        QuantLib::Option::Type _optionType = (QuantLib::Option::Type)optionType ;
        _Cforward = safe_cast<CQuote^> (forward);
        _Cforward->Lock();
        Handle<QuantLib::Quote>& _forward = static_cast<Handle<QuantLib::Quote>&> (_Cforward->GetHandle ()); 
        _Cprice = safe_cast<CQuote^> (price);
        _Cprice->Lock();
        Handle<QuantLib::Quote>& _price = static_cast<Handle<QuantLib::Quote>&> (_Cprice->GetHandle ()); 
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        QuantLib::Real _guess = (QuantLib::Real)ValueHelper::Convert (guess); //d
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-6); //4
        QuantLib::Natural _maxIter = 
            (Microsoft::FSharp::Core::FSharpOption<UInt32>::IsSome::get (maxIter) ? (QuantLib::Natural)ValueHelper::Convert (maxIter->Value) : 100); //4
        _ppImpliedStdDevQuote = new boost::shared_ptr<QuantLib::ImpliedStdDevQuote> (new QuantLib::ImpliedStdDevQuote ( _optionType,  _forward,  _price,  _strike,  _guess,  _accuracy,  _maxIter ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cforward != nullptr) _Cforward->Unlock();
        if (_Cprice != nullptr) _Cprice->Unlock();
    }
}
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (boost::shared_ptr<QuantLib::ImpliedStdDevQuote>& childNative, Object^ owner) : CQuote(CImpliedStdDevQuote::typeid)
{
#ifdef HANDLE
	_phImpliedStdDevQuote = NULL;
#endif
	_ppImpliedStdDevQuote = &childNative;
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
}
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (QuantLib::ImpliedStdDevQuote& childNative, Object^ owner) : CQuote(CImpliedStdDevQuote::typeid)
{
#ifdef HANDLE
	_phImpliedStdDevQuote = NULL;
#endif
	_ppImpliedStdDevQuote = new boost::shared_ptr<QuantLib::ImpliedStdDevQuote> (&childNative);
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
    _ImpliedStdDevQuoteOwner = owner;
    _QuoteOwner = owner;
}

Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (CImpliedStdDevQuote^ copy) : CQuote(CImpliedStdDevQuote::typeid)
{
#ifdef HANDLE
	_phImpliedStdDevQuote = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppImpliedStdDevQuote = new boost::shared_ptr<QuantLib::ImpliedStdDevQuote> (copy->GetShared());
        _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
    }
}
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (PLATFORM::Type^ t) : CQuote(CImpliedStdDevQuote::typeid)
{
#ifdef HANDLE
	_phImpliedStdDevQuote = NULL;
#endif
	if (!t->IsSubclassOf(CImpliedStdDevQuote::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (QuantLib::Handle<QuantLib::ImpliedStdDevQuote>& childNative, Object^ owner)  : CQuote(CImpliedStdDevQuote::typeid)
{
	_phImpliedStdDevQuote = &childNative;
	_ppImpliedStdDevQuote = &static_cast<boost::shared_ptr<QuantLib::ImpliedStdDevQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
    _ImpliedStdDevQuoteOwner = owner;
}
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (QuantLib::Handle<QuantLib::ImpliedStdDevQuote> childNative)  : CQuote(CImpliedStdDevQuote::typeid)
{
	_phImpliedStdDevQuote = &childNative;
	_ppImpliedStdDevQuote = &static_cast<boost::shared_ptr<QuantLib::ImpliedStdDevQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
}
#endif
#ifdef STRUCT
Cephei::QL::Quotes::CImpliedStdDevQuote::CImpliedStdDevQuote (QuantLib::ImpliedStdDevQuote childNative)  : CQuote(CImpliedStdDevQuote::typeid)
{
#ifdef HANDLE
	_phImpliedStdDevQuote = NULL;
#endif
	_ppImpliedStdDevQuote = new boost::shared_ptr<QuantLib::ImpliedStdDevQuote> (new QuantLib::ImpliedStdDevQuote (childNative));
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppImpliedStdDevQuote));
}
#endif

Cephei::QL::Quotes::CImpliedStdDevQuote::~CImpliedStdDevQuote ()
{
    if (_ppImpliedStdDevQuote != NULL)
    {
	    delete _ppImpliedStdDevQuote;
        _ppImpliedStdDevQuote = NULL;
    }
}
Cephei::QL::Quotes::CImpliedStdDevQuote::!CImpliedStdDevQuote ()
{
    if (_ppImpliedStdDevQuote != NULL)
    {
	    delete _ppImpliedStdDevQuote;
    }
}
QuantLib::ImpliedStdDevQuote& Cephei::QL::Quotes::CImpliedStdDevQuote::GetReference ()
{
    if (_ppImpliedStdDevQuote == NULL) throw REFNEW NativeNullException ();
	return **_ppImpliedStdDevQuote;
}
boost::shared_ptr<QuantLib::ImpliedStdDevQuote>& Cephei::QL::Quotes::CImpliedStdDevQuote::GetShared ()
{
    if (_ppImpliedStdDevQuote == NULL) throw REFNEW NativeNullException ();
	return *_ppImpliedStdDevQuote;
}
QuantLib::ImpliedStdDevQuote* Cephei::QL::Quotes::CImpliedStdDevQuote::GetPointer ()
{
    if (_ppImpliedStdDevQuote == NULL) throw REFNEW NativeNullException ();
	return &**_ppImpliedStdDevQuote;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ImpliedStdDevQuote>& Cephei::QL::Quotes::CImpliedStdDevQuote::GetHandle ()
{
	if (_phImpliedStdDevQuote == NULL)
	{
		_phImpliedStdDevQuote = new Handle<QuantLib::ImpliedStdDevQuote> (*_ppImpliedStdDevQuote);
	}
	return *_phImpliedStdDevQuote;
}
#endif
bool Cephei::QL::Quotes::CImpliedStdDevQuote::HasNative () 
{
	return (_ppImpliedStdDevQuote != NULL);
}

Boolean Cephei::QL::Quotes::CImpliedStdDevQuote::IsValid::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppImpliedStdDevQuote)->isValid ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Quotes::CImpliedStdDevQuote::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppImpliedStdDevQuote)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Quotes::IImpliedStdDevQuote^ Cephei::QL::Quotes::CImpliedStdDevQuote_Factory::Create (QL::Option::TypeEnum optionType, Cephei::QL::IQuote^ forward, Cephei::QL::IQuote^ price, Double strike, Double guess, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt32>^ maxIter)
{
    return REFNEW CImpliedStdDevQuote ( optionType,  forward,  price,  strike,  guess,  accuracy,  maxIter);
}
